package top.dreamix.njitkit.ui.screen.yiban

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import cafe.adriel.lyricist.LocalStrings
import top.dreamix.njitkit.data.yiban.Account
import top.dreamix.njitkit.ui.viewmodel.YibanViewModel

val modifierPadding16dp = Modifier.padding(16.dp)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun YibanUserAddDialog(
    onDismissRequest: () -> Unit,
    updateUser: (Account) -> Unit = {}
) {
    val strings = LocalStrings.current
    val scope = rememberCoroutineScope()
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    fun confirmAccount() {
        updateUser(Account(
            user_id = -1,
            phone = username,
            password = password
        ))
        onDismissRequest()
    }

    Dialog(
        onDismissRequest = onDismissRequest,
    ) {
        Card {
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Spacer(Modifier.height(8.dp))
                OutlinedTextField(
                    modifier = Modifier
                        .padding(16.dp, 8.dp)
                        .fillMaxWidth(),
                    value = username,
                    label = {Text(strings.username)},
                    onValueChange = { username = it },
                )
                OutlinedTextField(
                    modifier = Modifier
                        .padding(16.dp, 8.dp)
                        .fillMaxWidth(),
                    value = password,
                    label = {Text(strings.password)},
                    onValueChange = { password = it },
                    visualTransformation = PasswordVisualTransformation()
                )
                Spacer(Modifier.height(8.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.End
                ) {
                    Button(
                        onClick = {
                            if (username.isEmpty() || password.isEmpty()) return@Button
                            confirmAccount()
                            onDismissRequest()
                        }
                    ) {
                        Text(strings.confirm)
                    }
                    Spacer(Modifier.width(16.dp))
                }
                Spacer(Modifier.height(16.dp))
            }
        }
    }
}

@Preview
@Composable
private fun PreviewUserAdd(){
    YibanUserAddDialog(
        {},
        {  }
    )
}
